package com.vgooo.client.vchannel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.LinearLayout.LayoutParams;

import com.umeng.analytics.MobclickAgent;
import com.vgooo.bean.Brand;
import com.vgooo.bean.NewListInfo;
import com.vgooo.client.R;
import com.vgooo.client.VGActivity;
import com.vgooo.client.ui.TabPageIndicator;
import com.vgooo.client.ui.VChannelFragment;
import com.vgooo.util.GSonTools;
import com.vgooo.util.HttpUtils;

public class VChannelFragmentActivity extends VGActivity {

	private final static String className = "VChannelFragmentActivity";// 获取类名，用于调试
	private List<Brand> brands ;// 品牌信息

	private List<NewListInfo> news;// 视频信息列表


	private boolean isFirst = true;

	private TabPageIndicator indicatior;
	private ViewPager viewPager;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		detailVeiw = inflater.inflate(R.layout.v_channel_main_fragment, null);
		setContentView(detailVeiw);
		initView();
		needNewLoading = true;
		brands = new ArrayList<Brand>();
		
		FragmentPagerAdapter fragmentAdapter = new VChannelFragmentAdapter(
				getSupportFragmentManager());
		viewPager.setOffscreenPageLimit(3);
		viewPager.setAdapter(fragmentAdapter);
		indicatior.setViewPager(viewPager);
	}
	
	
	public void initView() {
		indicatior = (TabPageIndicator) findViewById(R.id.brands_indicator);
		viewPager = (ViewPager) findViewById(R.id.mobiles_viewPager);
		indicatior.setMoreBrands(findViewById(R.id.v_channel_main_brands_sort_more));
		
	}
	

	class VChannelFragmentAdapter extends FragmentPagerAdapter {
		public VChannelFragmentAdapter(FragmentManager fm) {
			super(fm);
		}
		
		@Override
		public Fragment getItem(int position) {
			Brand brand = brands.get(position);			
			
			return VChannelFragment.newInstance(brand, news, VChannelFragmentActivity.this);
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return brands.get(position % brands.size()).brand_name;
		}

		@Override
		public int getCount() {
			return brands.size();
		}
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		init() ;
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		// 友盟统计功能，打开页面
		MobclickAgent.onResume(this);
		if(news==null||news.isEmpty()){
			init();
		}

	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		// 友盟统计功能，页面关闭
		MobclickAgent.onPause(this);
		// looping.stop();
	}
	
	protected void onDestroy() {
		super.onDestroy();
	};

	LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
			LayoutParams.FILL_PARENT);

	@Override
	public void setupView(){
		// TODO Auto-generated method stub	

		indicatior.notifyDataSetChanged();	
	}
	
	@Override
	public void getData()  {
		try {
			// TODO Auto-generated method stub
			String urlPath;
			String jsonString;

			// 获取品牌信息
			if (isFirst) {
				urlPath = HttpUtils.BASE_URI + "bartags?type=n";
				jsonString = HttpUtils.getJsonString(urlPath);
				List<Map<String, Object>> list = GSonTools
						.getMulObjectList(jsonString);
				brands = new ArrayList<Brand>();
				Brand b = new Brand();
				b.brand_id = "0";
				b.brand_name = "全部";
				brands.add(b);
				for (Map<String, Object> map : list) {
					Brand brand = new Brand();
					brand.brand_id = (String) map.get("bt_id");
					brand.brand_name = (String) map.get("bt_tag");
					brands.add(brand);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e) ;
		}

	}

}
